using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using Microsoft.Practices.ServiceLocation;

namespace PrismNavigationFramework.Navigation.Controls
{
    public class PrismNavigationMainFrame : TransitionContentControl, INavigate
    {
        public PrismNavigationMainFrame()
        {
            if (IsDesignTime)
            {
                return;
            }

            this.Loaded += PrismNavigationFrame_Loaded;
        }

        static void PrismNavigationFrame_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            Application.Current.Host.NavigationStateChanged += OnNavigationStateChanged;

            if (string.IsNullOrEmpty(Application.Current.Host.NavigationState))
            {
                NavigationStateChanged(string.Format("/{0}/{1}", PrismNavigationConfiguration.
                                                                                DefaultModuleName,
                                                                            PrismNavigationConfiguration.
                                                                                DefaultViewName));
            }
            else if (Application.Current.Host.NavigationState.ToLower() != PrismViewLoadingService.CurrentMainRegionUrl.ToLower())
            {
                // this is a hack to force our view to be loaded if user reloads page in browser
                NavigationStateChanged(Application.Current.Host.NavigationState);
            }

        }

        private static void NavigationStateChanged(string newUrl)
        {
            if (newUrl.ToLower() != PrismViewLoadingService.CurrentMainRegionUrl.ToLower())
            {
                PrismViewLoadingService.LoadUrlToRegion(newUrl, PrismNavigationConfiguration.MainRegionName);
            }
        }

        private static void OnNavigationStateChanged(object sender, NavigationStateChangedEventArgs e)
        {
            NavigationStateChanged(e.NewNavigationState);
        }

        private static bool IsDesignTime
        {
            get
            {
                return DesignerProperties.IsInDesignTool;
            }
        }

        private static IPrismViewLoadingService PrismViewLoadingService
        {
            get
            {
                if (IsDesignTime)
                {
                    return null;
                }
                return ServiceLocator.Current.GetInstance<IPrismViewLoadingService>();
            }
        }

        private static IPrismNavigationConfiguration PrismNavigationConfiguration
        {
            get
            {
                if (IsDesignTime)
                {
                    return null;
                }
                return ServiceLocator.Current.GetInstance<IPrismNavigationConfiguration>();
            }
        }


        public bool Navigate(Uri source)
        {
            Application.Current.Host.NavigationState = source.OriginalString;
            return true;
        }
    }
}